export const block: ICustomBlockOption = {
    name: "SToyEdgeDetection1",
    comments: "实现ShaderToy边缘检测效果",
    document: "https://www.shadertoy.com/view/4tScDD",
    target: "Neutral",
    inParameters: [
        {
            name: "uv",
            type: "Vector2"
        },
		{
            name: "time",
            type: "Float"
        },
		{
            name: "size",
            type: "Vector2"
        },
		{
            name: "source",
            type: "Object"
        }
    ],
    outParameters: [
        {
            name: "output",
            type: "Vector4"
        }
    ],
    "includes": [
        "shaderToyDeclaration"
    ],
    entryFunctionName: "SToyEdgeDetection1",
    code: `
		void SToyEdgeDetection1(vec2 fragCoord, float time, vec2 size, sampler2D iChannel0, out vec4 fragColor)
		{
			iTime = time;

            // 提供多种卷积内核
			const mat3 edgeDetectKernel = mat3(-1., -1., -1., -1., 8., -1., -1., -1., -1.);
            const mat3 gaussianBlurKernel = mat3(0.045, 0.122, 0.045, 0.122, 0.332, 0.122, 0.045, 0.122, 0.045);
            const mat3 unsharpenKernel = mat3(-1., -1., -1., -1., 9., -1., -1., -1., -1.);
            const mat3 sharpnessKernel = mat3(0., -1., 0., -1., 5., -1., 0., -1., 0.);

            const mat3 xKernel = mat3(-1., 0., 1., -1., 0., 1., -1., 0., 1.);
            const mat3 yKernel = mat3(-1., -1., -1., 0., 0., 0., 1., 1., 1.);

            vec2 uv1 = fragCoord.xy;
            float pixX = 1.0 / size.x;
            float pixY = 1.0 / size.y;
            float height = 0.5 + 0.5 * cos(iTime);
            //moving line
            vec4 colorSum = vec4(0);
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++){
                    vec4 a_color = texture(iChannel0, vec2(uv1.x + pixX * xKernel[i][j], uv1.y + pixY * yKernel[i][j]));
                    //getting pixel colour from the texture
                    float intensity = a_color.x * 0.3 + a_color.y * 0.59 + a_color.z * 0.11;

                    // 这里使用 edgeDetectKernel
                    colorSum += vec4(intensity) * edgeDetectKernel[i][j];
                    //weighted summation
                }

            }
            if (uv1.y > height) {
                vec4 a_color = texture(iChannel0, uv1);
                float intensity = a_color.x * 0.3 + a_color.y * 0.59 + a_color.z * 0.11;
                // 灰度图
                fragColor = vec4(intensity);
                // //原图
                // fragColor = texture(iChannel0, uv1);
            } else {
                //卷积内核处理效果
                fragColor = colorSum;
            }
		}
    `
}